// JavaScript Document
function afterIssuePutniNalog(response){
	var tmp = response.split("{!}",2);
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	alert(msg);
	if(!code){
		LoadRSDoc("putni_nalozi.php","setContent");
	}
}
function afterEnterPutniNalog(response){
	var tmp = response.split("{!}",2);
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	alert(msg);
}

function setRegBr(sel){
	var gar=sel.value;
	var reg = sel.nextSibling.nextSibling;
	var opts = reg.options;
	for(var i=0; i<opts.length; i++){
		var g = opts[i].getAttribute("gar");
		if(g==gar){
			opts[i].selected = true;
		}else{
			opts[i].selected = false;
		}
	}
}
function pnEnterAddRow(btn){
	var tbl = btn.parentNode.parentNode.parentNode;
	var trs = tbl.getElementsByTagName("tr");
	var ltr = trs[trs.length-1];
	var index = parseInt(ltr.id);
	
	var cl = ltr.cloneNode(true);
	index++;
	cl.id = index;
	tbl.appendChild(cl);
	
	var ldest = ltr.getElementsByTagName("textarea")[1];
	
	
	var els = Array("input","textarea");
	for(var j=0; j<els.length; j++){
		var inpts = cl.getElementsByTagName(els[j]);
		for(var i=0; i<inpts.length; i++){
			if(inpts[i].name.length){
				var base = inpts[i].name.substr(0,inpts[i].name.lastIndexOf("_"));
				inpts[i].name = base+"_"+index;
				inpts[i].id = inpts[i].name;
				inpts[i].value = "";
				
				if(inpts[i].name.match(/^relacija_od_/)){
					inpts[i].value = ldest.value;
				}
			}
		}
	}
}
function pnEnterDelRow(btn){
	var tr = btn.parentNode.parentNode;
	var tbl = tr.parentNode;
	var trs = tbl.getElementsByTagName("tr");
	if(trs.length>3){
		var len = trs.length-2;
		if(confirm("Da li ste sigurni da želite da obrišete odabrani red?")){
			var index = parseInt(tr.id);
			tbl.removeChild(tr);
			for(var i=index+1; i<=len; i++){
				var xtr = document.getElementById(i);
				xtr.id = i-1;
				
				var els = Array("input","textarea");
				for(var j=0; j<els.length; j++){
					var inpts = xtr.getElementsByTagName(els[j]);
					for(var j=0; j<inpts.length; j++){
						if(inpts[j].name.length){
							var base = inpts[j].name.substr(0,inpts[j].name.lastIndexOf("_"));
							inpts[j].name = base+"_"+(i-1);
							inpts[j].id = inpts[j].name;
						}
					}
				}
			}
		}
	}
	
}

function pnTestEnterForm(){
	var tbl = document.getElementById("enterTable");
	var trs = tbl.getElementsByTagName("tr");
	
	var ret = true;
	if(trs.length>=3){
		var pps = parseInt(trim(document.getElementById("prethodna_km").value));
		if(pps>0){
			var fpredj = document.getElementById("predjeno_1");
			var fstanj = document.getElementById("stanje_1");
			var fnumkm = parseInt(trim(fpredj.value));
			var fst = parseInt(trim(fstanj.value));
			if((pps+fnumkm) != fst){
				alert("Označena kilometraža se ne podudara!");
				fstanj.focus();
				fstanj.select();
				return false;
			}
		}
		if(trs.length>3){
			for(var i=2; i<=(trs.length-2); i++){
				var prev_stanje = document.getElementById("stanje_"+(i-1));
				var numkm = document.getElementById("predjeno_"+i);
				var stanje = document.getElementById("stanje_"+i);
				
				var ps = parseInt(trim(prev_stanje.value)); ps = ps>0?ps:0;
				var nk = parseInt(trim(numkm.value)); nk = nk>0?nk:0;
				var st = parseInt(trim(stanje.value)); st = st>0?st:0;
				
				if((ps+nk) != st){
					ret = false;
					alert("Označena kilometraža se ne podudara!");
					stanje.focus();
					stanje.select();
					break;
				}
			}
		}
	}
	return ret;
}

function updatePrethodnostanjeBrojila(response){
	document.getElementById("prethodna_km").value = response;
	document.getElementById("broj").focus();
	document.getElementById("broj").select();
}
function updateReportPutniNalozi(response){
	document.getElementById("PNReportResult").innerHTML = response;
}
function sumKM(input){
	var index = input.name.substr(input.name.lastIndexOf("_")+1);
	var val = input.value;
	val = val.length>0?parseInt(val):0;
	
	index = parseInt(index);
	var startState=0;
	if(index==1){
		startState = document.getElementById("prethodna_km").value;
		startState = startState.length>0?parseInt(startState):0;
	}else{
		startState = document.getElementById("stanje_"+(index-1)).value;
		startState = startState.length>0?parseInt(startState):0;
	}
	
	document.getElementById("stanje_"+index).value = startState+val;
}